package cn.coders.haoniu.caipiao;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.coders.net.application.utils.CnCoderFileUtil;


@Controller
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableScheduling
@ComponentScan(basePackages = "cn.coders.haoniu.caipiao")
@ServletComponentScan(basePackages = "cn.coders.haoniu.caipiao")
public class App {
    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "OK";
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        int processId = getProcessID();
        String pidfile = null;
        for (String tem : args) {
            if (tem.startsWith("-pid=")) {
                pidfile = tem.substring(5, tem.length());
            }
        }
        if (StringUtils.isNotBlank(pidfile)) {
            CnCoderFileUtil.writeString2File(String.valueOf(processId), pidfile);
        }
        SpringApplication.run(App.class, args);
    }


    public static final int getProcessID() {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        return Integer.valueOf(runtimeMXBean.getName().split("@")[0]).intValue();
    }

}
